package es.capgemini.extjsplugin.ui;

import java.lang.reflect.Field;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.swt.widgets.TableItem;

public class ExampleCellModifier implements ICellModifier {

	private AttributesSelectionPanel tableViewerExample;
	private String[] columnNames;
	
	/**
	 * Constructor 
	 * @param TableViewerExample an instance of a TableViewerExample 
	 */
	public ExampleCellModifier(AttributesSelectionPanel tableViewerExample) {
		super();
		this.tableViewerExample = tableViewerExample;
	}

	/**
	 * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
	 */
	public boolean canModify(Object element, String property) {
		return true;
	}

	/**
	 * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
	 */
	public Object getValue(Object element, String property) {

		// Find the index of the column
		
		int columnIndex = tableViewerExample.getColumnNames().indexOf(property);

		Object result = null;
		Field field = (Field) element;

		switch (columnIndex) {
			case 0 : // COMPLETED_COLUMN 
				result = new Boolean(true);
				break;
			case 1 : // DESCRIPTION_COLUMN 
				result = field.getName();
				break;
			case 2 : // OWNER_COLUMN 
				String stringValue = field.getType().getSimpleName();
//				String[] choices = tableViewerExample.getChoices(property);
//				int i = choices.length - 1;
//				while (!stringValue.equals(choices[i]) && i > 0)
//					--i;
//				result = new Integer(i);					
				result=stringValue;
				break;

			default :
				result = "";
		}
		return result;	
	}

	/**
	 * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
	 */
	public void modify(Object element, String property, Object value) {	

		// Find the index of the column 
		int columnIndex	= tableViewerExample.getColumnNames().indexOf(property);
			
		TableItem item = (TableItem) element;
//		ExampleTask task = (ExampleTask) item.getData();
		Field field = (Field)item.getData();
		String valueString;

		switch (columnIndex) {
			case 0 : // COMPLETED_COLUMN 
			    //task.setCompleted(((Boolean) value).booleanValue());
				break;
			case 1 : // DESCRIPTION_COLUMN 
				valueString = ((String) value).trim();
				//task.setDescription(valueString);
				break;
			case 2 : // OWNER_COLUMN 
//				valueString = tableViewerExample.getChoices(property)[((Integer) value).intValue()].trim();
//				if (!task.getOwner().equals(valueString)) {
//					task.setOwner(valueString);
//				}
				break;
			
			default :
			}
		//tableViewerExample.getTaskList().taskChanged(task);
	}

}
